#include "dir.h"

void print_test_result(session_t *session, int ret, const char *buf){
    if(ret == 0){
        printf("状态:成功.\n");
    }else{
        printf("状态:失败.\n");
    }
    printf("返回信息: %s \n",buf);
    printf("当前虚拟地址: %s\n",session->curVirPath);
    printf("-------结束-------\n");
}

int main(){
    MYSQL *mysql = mysql_init(NULL);
    MYSQL *conn_ret = mysql_real_connect(mysql,
                                    "localhost",
                                    "root",
                                    "123",
                                    "testDir",
                                    0,NULL,0);
    if(conn_ret == NULL){
        fprintf(stderr, "%s:%s\n", "mysql_real_connect", mysql_error(mysql));
        return -1;
    }

    session_t session;
    char buf[4096] = {0};
    int ret;

    printf("=======init_session测试=======\n");
    ret = init_session(&session,"张三",mysql);
    print_test_result(&session, ret, buf);

    printf("=======pwd测试=======\n");
    ret = handle_pwd(&session, buf, sizeof(buf));
    print_test_result(&session, ret, buf);

    //printf("=======ls测试=======\n");
    //ret = handle_ls(&session, mysql, NULL, buf, sizeof(buf));
    //printf("无参数. 预期 dir1/ file1 \n");
    //print_test_result(&session, ret, buf);
    //
    //ret = handle_ls(&session, mysql, "/", buf, sizeof(buf));
    //printf("参数/. 预期 dir1/ file1 \n");
    //print_test_result(&session, ret, buf);
    //
    //ret = handle_ls(&session, mysql, "/dir1", buf, sizeof(buf));
    //printf("参数/dir1. 预期 dir2/ \n");
    //print_test_result(&session, ret, buf);
   
    //ret = handle_ls(&session, mysql, "/aaa", buf, sizeof(buf));
    //printf("不存在参数/aaa. 预期报错 \n");
    //print_test_result(&session, ret, buf);
    //
    printf("=======cd测试=======\n");
    ret = handle_cd(&session, mysql, "/dir1", buf, sizeof(buf));
    printf("参数/dir1. 预期 pwd /dir1 \n");
    print_test_result(&session, ret, buf);

    ret = handle_cd(&session, mysql, "dir4", buf, sizeof(buf));
    printf("参数dir4. 相对路径 预期 pwd /dir1/dir4 \n");
    print_test_result(&session, ret, buf);

    ret = handle_cd(&session, mysql, "/dir1/dir2", buf, sizeof(buf));
    printf("参数/dir1/dir2. 预期 pwd /dir1/dir2 \n");
    print_test_result(&session, ret, buf);
    
    ret = handle_cd(&session, mysql, NULL, buf, sizeof(buf));
    printf("无参数. 预期 pwd / \n");
    print_test_result(&session, ret, buf);
    
    ret = handle_cd(&session, mysql, "/aaa", buf, sizeof(buf));
    printf("错误参数/aaa. 预期报错 / \n");
    print_test_result(&session, ret, buf);
    //
    //printf("=======mkdir->ls测试=======\n");
    //ret = handle_mkdir(&session, mysql, "/dir3", buf, sizeof(buf));
    //printf("参数/dir3. 预期 ls dir1/ dir3/ file1 \n");
    //ret = handle_ls(&session, mysql, "/", buf, sizeof(buf));
    //print_test_result(&session, ret, buf);
    //
    //ret = handle_mkdir(&session, mysql, NULL, buf, sizeof(buf));
    //printf("无参数. 预期报错 \n");
    //print_test_result(&session, ret, buf);
    //
    //ret = handle_mkdir(&session, mysql, "/dir1/dir4", buf, sizeof(buf));
    //printf("参数/dir1/dir4. 预期 ls dir2/ dir4/ \n");
    //ret = handle_ls(&session, mysql, "/dir1", buf, sizeof(buf));
    //print_test_result(&session, ret, buf);

    //ret = handle_mkdir(&session, mysql, "////", buf, sizeof(buf));
    //printf("错误参数. 预期报错 \n");
    //print_test_result(&session, ret, buf);

    //printf("=======remove->ls测试=======\n");
    //ret = handle_remove(&session, mysql, NULL, buf, sizeof(buf));
    //printf("无参数. 预期报错 \n");
    //print_test_result(&session, ret, buf);
    //
    //ret = handle_remove(&session, mysql, "/dir1/dir2/rmfile1", buf, sizeof(buf));
    //printf("参数/dir1/dir2/rmfile1. 预期 ls 空  \n");
    //ret = handle_ls(&session, mysql, "/dir1/dir2", buf, sizeof(buf));
    //print_test_result(&session, ret, buf);
    //
    //ret = handle_remove(&session, mysql, "/dir1/dir2/rmfile1", buf, sizeof(buf));
    //printf("错误参数/dir1/dir2/rmfile1. 预期报错  \n");
    //print_test_result(&session, ret, buf);
    //
    //ret = handle_remove(&session, mysql, "/////", buf, sizeof(buf));
    //printf("错误参数/////. 预期报错  \n");
    //print_test_result(&session, ret, buf);
    
    //printf("=======rmdir->ls测试=======\n");
    //ret = handle_rmdir(&session, mysql, NULL, buf, sizeof(buf));
    //printf("无参数. 预期报错 \n");
    //print_test_result(&session, ret, buf);
    //
    //ret = handle_rmdir(&session, mysql, "/////", buf, sizeof(buf));
    //printf("无参数. 预期报错 \n");
    //print_test_result(&session, ret, buf);
    //
    //ret = handle_rmdir(&session, mysql, "/dir1/dir2", buf, sizeof(buf));
    //printf("参数/dir1/dir2. 预期 删除成功 \n");
    //print_test_result(&session, ret, buf);
    //
    //ret = handle_rmdir(&session, mysql, "/dir1/dir4", buf, sizeof(buf));
    //printf("参数/dir1/dir4/. 预期 报错非空目录 \n");
    //print_test_result(&session, ret, buf);
    mysql_close(mysql);
    return 0;
}
